﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.Profile;

public partial class User_Shares_QuestionAnswer : System.Web.UI.Page
{
    protected void Page_Load( object sender, EventArgs e )
    {
        LblStatus.Text = "";
        try
        {
            SqlDataSourceEdit.ConnectionString = DataAccess.Global.GetConnectionString();
            SqlDataSourceQuestions.ConnectionString = DataAccess.Global.GetConnectionString();
            SqlDataSourceView.ConnectionString = DataAccess.Global.GetConnectionString();
            //GridviewQuestions.DataBind();
            //DetailsView1.DataBind();
            var thisUser = HttpContext.Current.User;
            if (thisUser.Identity.IsAuthenticated)
            {
                string id = Request.QueryString["id"].ToString();

                MultiView1.ActiveViewIndex = Convert.ToInt32(id);
            }
            else
            {
                MultiView1.ActiveViewIndex = -1;
            }
            //DetailsView1.DataSource = AccessLayer.ProDetail.Find(p);
            //   //DetailsView1.DataSource= AccessLayer.ProDetail.FetchByID(Convert.ToInt32(code));
            //   DetailsView1.DataBind();
        }
        catch
        {
        }
    }

    protected void Button1_Click( object sender, EventArgs e )
    {
        try
        {
            if ( TxtQuestion.Text != "" )
            {
                DataAccess.TblQuestion q = new DataAccess.TblQuestion();
                q.Question = TxtQuestion.Text;
                q.UserName = Membership.GetUser().UserName;
                q.DateX = DateTime.Now;
                q.Save();
                MultiView1.ActiveViewIndex = -1;
                LblStatus.Text = " <script>alert('.سوال شما با موفقیت ارسال شد')</script>";
            }
            else
            {
                LblStatus.Text = " <script>alert('.لطفا متن سوال را وارد کنید')</script>";
            }
        }
        catch
        {
            LblStatus.Text = " <script>alert('.خطا در ارسال سوال,لطفا مجددا سعی کنید')</script>";
        }
    }

    protected void GridviewQuestions_RowCommand( object sender, GridViewCommandEventArgs e )
    {
        int id = Convert.ToInt32( e.CommandArgument );
        switch ( e.CommandName )
        {
            case "Select":
                Guid questionid = new Guid( GridviewQuestions.DataKeys[ id ].Value.ToString() );
                GridviewQuestions.Rows[ id ].BackColor = System.Drawing.Color.Gold;
                GridviewQuestions.Enabled = false;
                Session[ "questionid" ] = questionid;
                PanelAnswer.Visible = true;
                DataAccess.TblQuestion qu = DataAccess.TblQuestion.FetchByID( questionid );
                LblQuestion.Text = qu.Question;
                GridviewQuestions.Enabled = false;
                break;
        }
    }

    protected void BtnSendAnswer_Click( object sender, EventArgs e )
    {
        try
        {
            if ( TxtAnswer.Text != "" )
            {
                DataAccess.TblAnswer a = new DataAccess.TblAnswer();
                a.Answer = TxtAnswer.Text;
                a.DateX = DateTime.Now;
                a.QuestionID = new Guid( Session[ "questionid" ].ToString() );
                a.UserName = Session[ "Username" ].ToString();
                a.Save();
                LblStatus.Text = " <script>alert('.پاسخ شما با موفقیت ارسال شد')</script>";
                TxtAnswer.Text = "";
                GridviewQuestions.Attributes.Clear();
                GridviewQuestions.Enabled = true;
                PanelAnswer.Visible = false;
            }
            else
            {
                LblStatus.Text = " <script>alert('.لطفا متن پاسخ را وارد کنید')</script>";
            }
        }
        catch
        {
            LblStatus.Text = " <script>alert('.خطا در ارسال پاسخ,لطفا مجددا سعی کنید')</script>";
        }
    }

    protected void GridviewQuestions_RowDataBound( object sender, GridViewRowEventArgs e )
    {
           if ( e.Row.RowType
             == DataControlRowType.DataRow )
        {
            string pdate = new Business.Calender().MiladiToPersian( Convert.ToDateTime( e.Row.Cells[ 4 ].Text ) );
            e.Row.Cells[4].Text = pdate;
            IDataReader dr= DataAccess.TblUserDetail.FetchByParameter(DataAccess.TblUserDetail.Columns.UserName, (e.Row.Cells[3].Text));
            if(dr.Read())
            {
                 if(dr["Name"].ToString()!="")
                  {
                    if (dr["Role"].ToString() == "Advisorhoghooghi")
                    {
                       e.Row.Cells[3].Text ="شرکت "+ dr["Name"].ToString();
                    }
                   else
                    {
                       e.Row.Cells[3].Text = dr["Name"].ToString() +" "+ dr["Family"].ToString();
                    }  
                  }
                
                  else
                  {
                   e.Row.Cells[3].Text= "نام فرد ثبت نشده";
                  }
                    dr.Close();
                }
      

        }
        if ( e.Row.RowType == DataControlRowType.DataRow )
        {
            e.Row.Attributes[ "onmouseover" ] = "oroginalcolor=this.style.backgroundColor;this.style.backgroundColor='#3399FF';";
            e.Row.Attributes[ "onmouseout" ] = "this.style.backgroundColor=oroginalcolor;";
        }
    }

    protected void Button1_Click1( object sender, EventArgs e )
    {
        TxtAnswer.Text = "";
        GridviewQuestions.Attributes.Clear();
        GridviewQuestions.Enabled = true;
        PanelAnswer.Visible = false;
    }

    protected void ListView1_ItemDataBound( object sender, ListViewItemEventArgs e )
    {
        //if (e.Item.ItemType == )
        //{
        //    string pdate = new Business.Calender().MiladiToPersian(Convert.ToDateTime(e.Row.Cells[4].Text));
        //    e.Row.Cells[4].Text = pdate;
        //}

        if ( e.Item.ItemType
             == ListViewItemType.DataItem )
        {
            //ListViewDataItem dataitem = (ListViewDataItem)e.Item;
            //DateTime id = Convert.ToDateTime(DataBinder.Eval(dataitem.DataItem, "[تاریخ طرح سوال]"));
            //string pdate = new Business.Calender().MiladiToPersian(id);
            //Label questiondate = (Label)e.Item.FindControl("تاریخ_طرح_سوالLabel");
           
            ////string pdate = new Business.Calender().MiladiToPersian(Convert.ToDateTime(questiondate.Text));
            //questiondate.Text = pdate;
            //dataitem = pdate;
            //if(policyid == 3)          
            //    //what should I write here to change the corresponding row's color?   
            //}
        }
    }

    protected void GridViewEdit_RowDataBound( object sender, GridViewRowEventArgs e )
    {
      
        if ( e.Row.RowType
             == DataControlRowType.DataRow )
        {
            e.Row.Attributes[ "onmouseover" ] = "oroginalcolor=this.style.backgroundColor;this.style.backgroundColor='#3399FF';";
            e.Row.Attributes[ "onmouseout" ] = "this.style.backgroundColor=oroginalcolor;";
        }
    }

    protected void GridView1_RowCreated( object sender, GridViewRowEventArgs e )
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    e.Row.Cells[0].Attributes.Add("onclick", "return confirm('آيا از حذف مطمئن هستيد؟');");
        //}
    }
    protected void ListView1_DataBound(object sender, EventArgs e)
    {
        //if (e.ItemType == ListViewItemType.DataItem)
        //{
        //}
    }
}